﻿// Screenshotz
//
// Copyright © Frank Nagl, 2008-2012
// admin@franknagl.de
// www.franknagl.de
//
namespace Screenshotz
{
    using System;
    using System.Diagnostics;
    using System.Windows.Forms;

    /// <summary> Provides GUI for entering a string in a text box.</summary>
    public partial class StringDialog : Form
    {
        private readonly Stopwatch watch;

        /// <summary>
        /// Initializes a new instance of the <see cref="StringDialog"/> class.
        /// </summary>
        /// <param name="caption">
        /// The caption of the <see cref="StringDialog"/> window.</param>
        /// <param name="label">The label text.</param>
        /// <param name="okButton">The text of the OK button.</param>
        /// <param name="cancelButton">The text of the cancel button.</param>
        /// <param name="textbox">The offered text in the textbox.</param>
        protected StringDialog(
            string caption, 
            string label,              
            string okButton, 
            string cancelButton,
            string textbox = "")
        {
            InitializeComponent();
            watch = new Stopwatch();
            watch.Start();

            Name = caption;
            this.label.Text = label;
            btnOK.Text = okButton;
            btnCancel.Text = cancelButton;
            this.textbox.Text = textbox;
        }

        /// <summary> Shows the string dialog. </summary>
        /// <param name="s">The string to use in 
        /// <see cref="textbox"/>.</param>
        /// <param name="caption">
        /// The caption of the <see cref="StringDialog"/> window.</param>
        /// <param name="label">The label text.</param>
        /// <param name="okButton">The text of the OK button.</param>
        /// <param name="cancelButton">The text of the cancel button.</param>
        /// <param name="textbox">The offered text in the textbox.</param>
        /// <returns>The dialog result after user interaction.</returns>
        public static DialogResult Show(
            ref string s,
            string caption, 
            string label,      
            string textbox = "",
            string okButton = "OK",
            string cancelButton = "Cancel")
        {
            StringDialog dialog = new StringDialog(
                caption, label, okButton, cancelButton, textbox);

            return GetDialogResult(dialog, ref s);
        }

        private static DialogResult GetDialogResult(
            StringDialog dialog, ref string s)
        {
            // getting information from parent
            dialog.textbox.Text = s;
            DialogResult dr = dialog.ShowDialog();
            // check user interaction
            if (dr == DialogResult.OK)
            {
                s = dialog.textbox.Text;
            }
            return dr;            
        }

        private void TextboxKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Return)
            {
                watch.Stop();
                TimeSpan end = watch.Elapsed;

                // to avoid automatically return press by MessageBoxCancel dialog
                if (end.Milliseconds > 200)
                {
                    btnOK.PerformClick();
                }
                else
                {
                    watch.Start();
                }
            }

        }
    }
}
